package com.baidu.shoppingmall.auth.controller;

import com.baidu.shoppingmall.auth.domain.CommentDTO;
import com.baidu.shoppingmall.auth.domain.CommentVO;
import com.baidu.shoppingmall.auth.service.CommentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

//@RestController
//@RequestMapping("/comment")
public class CommentController {


    private static final Logger logger= LoggerFactory.getLogger(CommentController.class);

    @Autowired
    private CommentService commentService;

    /**
     * 获取所有信息
     * @return
     */
    @GetMapping("/all")
    public List<CommentVO> getAll(){
        List<CommentVO> commentVO =new ArrayList<>();
        try {
            List<CommentDTO> commentDTOList=commentService.getAll();
            commentDTOList.forEach(dto-> commentVO.add(dto.clone(CommentVO.class)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return commentVO;
    }

    /**
     * 添加新的评论
     * @return
     */
    @PostMapping("/insert")
    public Integer insertNewComment(@RequestBody CommentVO commentVO){
        try {
            double total = commentVO.getAuthoritySimilarity() + commentVO.getAuthorityAttitudy() + commentVO.getAuthorityLogistics();
            commentVO.setAuthorityDybamic(Math.ceil(total/3));
            CommentDTO commentDTO = commentVO.clone(CommentDTO.class);
            Integer count=commentService.insertNewComment(commentDTO);

        } catch (Exception e) {
            logger.error("error", e);
            return 0;
        }
        return 1;
    }
}
